package cn.line5.seam.contexts;

import java.util.HashMap;
import java.util.Map;

import cn.line5.seam.webapp.UploadResult;

/**
 * <h1>文件处理上下文处理类</h1>
 * 
 * @author Liu,Xiaojun
 * @since JDK1.6
 */
public class UploadContext {

	private final static ThreadLocal<UploadResult> uploadResult = new ThreadLocal<UploadResult>();

	private final static ThreadLocal<Map<String, String>> parameter = new ThreadLocal<Map<String, String>>();

	/**
	 * 设置上传结果
	 */
	public static void setUploadResult(UploadResult result) {
		uploadResult.set(result);
	}

	/**
	 * 获取上传结果
	 * 
	 * @return
	 */
	public static UploadResult getUploadResult() {
		return uploadResult.get();
	}

	/**
	 * 清除上传结果
	 */
	public static void clearUploadResult() {
		uploadResult.remove();
	}

	/**
	 * 写入上传的其他参数
	 */
	public static void pubParameter(String name, String value) {
		if (parameter.get() == null) {
			parameter.set(new HashMap<String, String>());
		}
		parameter.get().put(name, value);
	}

	/**
	 * 判断是否包含参数
	 */
	public static boolean containParameter(String name) {
		return parameter.get() != null && parameter.get().containsKey(name);
	}

	/**
	 * 获取上传的其他参数
	 */
	public static String getParameter(String name) {
		return parameter.get() == null || !containParameter(name) ? null : parameter.get().get(name)
				.replace("@LSEAM-FGF@", ",");
	}

	/**
	 * 获取上传的其他参数数组
	 */
	public static String[] getParameterValues(String name) {
		return parameter.get() == null || !containParameter(name) ? null : parameter.get().get(name)
				.split("@LSEAM-FGF@");
	}

	/**
	 * 清除上传的其他参数
	 */
	public static void clearParameter() {
		parameter.remove();
	}

	/**
	 * 清除所有
	 */
	public static void clearAll() {
		parameter.remove();
		uploadResult.remove();
	}
}